package cn.zhbitzwz.com.testappbyid.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.zhbitzwz.com.testappbyid.AppInfo;
import cn.zhbitzwz.com.testappbyid.R;

/**
 * Created by ZWZ on 2017/4/9.
 */

public class AppAdapter extends BaseAdapter {
    Context context;
    ArrayList<AppInfo> dataList = new ArrayList<AppInfo>();

    public AppAdapter(Context context, ArrayList<AppInfo> inputDataList) {
        this.context = context;
        dataList.clear();
        for (int i = 0; i < inputDataList.size(); i++) {
            dataList.add(inputDataList.get(i));
        }
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        final AppInfo appUnit = dataList.get(position);
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.app_row, null);
            v.setClickable(true);
        }
        TextView appName = (TextView) v.findViewById(R.id.appName);
        ImageView appIcon = (ImageView) v.findViewById(R.id.icon);
        if (appName != null)
            appName.setText(appUnit.appName);
        if (appIcon != null)
            appIcon.setImageDrawable(appUnit.appIcon);
        return v;
    }
}
